package com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck;

import com.personal.gateway.custom.customException.ConfigurationOptionCheckException;
import com.personal.gateway.flinkAllCommand.configurationOption.ConfigurationOption;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.AbstractConfigurationType;
import org.apache.commons.lang3.StringUtils;

/**
 * @author BigHead
 * @date 2022/1/10 15:47
 * @description AbstractParameterCheck
 */
public abstract class AbstractParameterCheck<BT, CT extends AbstractConfigurationType<BT>> {

    public BT checkValue(ConfigurationOption<BT, CT> configurationOption, String value) {
        if (configurationOption.isNecessary()) {
            if (StringUtils.isEmpty(value)) {
                throw new ConfigurationOptionCheckException(value, configurationOption);
            }
        }
        BT targetValue = abstractCheckValue(configurationOption, value);
        if (configurationOption.isNecessary() && null == targetValue) {
            throw new ConfigurationOptionCheckException(value, configurationOption);
        } else {
            return targetValue;
        }
    }

    /**
     * 写入参数
     *
     * @param configurationOption 类型对象
     * @param value               字符串参数
     * @return 指定类型参数
     */
    public abstract BT abstractCheckValue(ConfigurationOption<BT, CT> configurationOption, String value);

}
